<template>
 <!-- 担保金额对话框 -->
    <el-dialog :title="queryParams.allName" :visible.sync="openGuaranteePrice" width="1200px" append-to-body :destroy-on-close="true" :before-close="beforeClose" >
      <el-form ref="form" :model="queryParams" :rules="rules" label-width="120px" :label-position="labelPosition">
        <el-table
          :data="guaranteePriceList"
          row-key="id"
          height="300"
          v-loading="loading"
        >
          <af-table-column prop="prrq" label="修改日期" fixed="left"></af-table-column>
          <af-table-column prop="shopname" label="修改人"></af-table-column>
          <af-table-column prop="jine" label="担保金额"></af-table-column>
          <af-table-column prop="jine2" label="提醒金额"></af-table-column>
          <af-table-column prop="state" label="当前状态"></af-table-column>
          <af-table-column prop="bxbl" label="保险比率(%)"></af-table-column>
          <af-table-column prop="scx" label="最终锁仓线"></af-table-column>
          <af-table-column prop="itbz" label="备注" width="150" :show-overflow-tooltip="true"></af-table-column>
        
        </el-table>
        <el-row class="mt20">
          <el-col :span="12">
            <el-form-item label="担保金额" prop="jine">
              <el-input v-model="queryParams.jine" placeholder="请输入担保金额" @input="changeJine()" type="number"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="保险比率(%)" prop="bxbl">
              <el-input v-model="queryParams.bxbl" placeholder="请输入保险比率(%)" @input="chageBxbl()" type="number" min="0"/>
            </el-form-item>
          </el-col>
           <el-col :span="12">
            <el-form-item label="提示比率(%)" prop="remindRate">
              <el-input v-model="remindRate" placeholder="请输入提示比率(%)"  @input="chageRemindRate()" type="number" min="0"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="最终锁仓线" prop="lockPrice">
              <el-input v-model="lockPrice" placeholder="请输入最终锁仓线" :disabled="true"/>
            </el-form-item>
          </el-col>
           <el-col :span="12">
            <el-form-item label="提示金额" prop="jine2">
              <el-input v-model="queryParams.jine2" placeholder="请输入提示金额" type="number"/>
            </el-form-item>
          </el-col>
        </el-row>
         <el-row>
          <el-col :span="12">
            <el-form-item label="备注" prop="itbz">
              <el-input v-model="queryParams.itbz" placeholder="备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="updateForm" v-hasPermi="['zhcloud:hwdb:modify']">修 改</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
</template>

<script>
import { listGuaranteePrice, modifyGuaranteedAmount } from "@/api/storage/guarantee";
import { AMOUNT_DIGIT } from '@/utils/constant'
import { getDigit } from "@/utils/amount"
import { priceFormat, delcommafy } from "@/utils/amount"


export default {
  name: "GuaranteePrice",
  data() {
    return {
      //对齐方式
      labelPosition:"right",
      //行数据
      rowData:{},
      // 显示搜索条件
      showSearch: true,
      // 重新渲染表格状态
      refreshTable: true,
      //弹窗状态
      openGuaranteePrice: false,
      //表单加载
      loading:true,
      //表单参数
      queryParams:{
        bxbl: "",
        isBa: "",
        itbz: "",
        jine: "",
        jine2: "",
        khmcid: "",
        khpb: 0,
        shopId: 0,
        spckidis: []
      },
      //提示比率
      remindRate:0,
      //最终仓锁线
      lockPrice:0,
      // 表单列表
      guaranteePriceList:[],
      // 表单校验
      rules: {
      },
      //页数
      pageSize: 10,
      //页码
      pageNum:1,
      // 总条数
      total: 0,
    };
  },
  watch: {
    'queryParams.jine':function(data) {
      this.queryParams.jine = getDigit(data, AMOUNT_DIGIT)
    },
    'queryParams.jine2':function(data) {
      this.queryParams.jine2 = getDigit(data, AMOUNT_DIGIT)
    },
  },
  methods: {
    /** 查询担保金额 */
    getList() {
      this.loading = true
      listGuaranteePrice({aid: this.rowData.id}).then(response => {
        this.loading = false
        const data = response.data
        for(let i in data) {
          data[i].jine = priceFormat(parseFloat(data[i].jine))
          data[i].jine2 = priceFormat(parseFloat(data[i].jine2))
          data[i].scx = priceFormat(parseFloat(data[i].scx))
        }
        this.guaranteePriceList = data
        this.total = response.total
      });
    },

    /**修改担保金额 */
    updateGuaranteedAmount() {
       modifyGuaranteedAmount(this.queryParams).then(response => {
        this.getList()
      });
    },
    //提示比率计算
    getRemindRate(row) {
      this.getConfigKey("Prompt_Ratio").then(response => {
        this.remindRate = response.msg;
        //this.queryParams.jine2 = (parseFloat(row.jine) /(parseFloat(this.remindRate)*0.01)).toFixed(AMOUNT_DIGIT)
      });
    },
    // 取消按钮
    cancel() {
      this.changeGuaranteePrice();
      this.resetQuery();
    },
    // 担保金额
    changeJine() {
      const jine = this.queryParams.jine
      this.lockPrice = parseFloat(parseFloat(jine) / ((parseFloat(this.queryParams.bxbl)) * 0.01)).toFixed(AMOUNT_DIGIT)
      this.queryParams.jine2 = (parseFloat(jine) /((parseFloat(this.remindRate)*0.01))).toFixed(AMOUNT_DIGIT)
    },
    //保险比率(%)
    chageBxbl() {
      const jine = this.queryParams.jine
      if(jine || jine === 0) {
        this.lockPrice = parseFloat(parseFloat(jine) / (parseFloat(this.queryParams.bxbl) * 0.01)).toFixed(AMOUNT_DIGIT)
      } else {
        this.lockPrice = ""
      }
    },

    //提示比率(%)
    chageRemindRate() {
      const jine = this.queryParams.jine
      if(jine || jine === 0) {
        this.queryParams.jine2 = (parseFloat(jine) /(parseFloat(this.remindRate)*0.01)).toFixed(AMOUNT_DIGIT)
      } else {
        this.queryParams.jine2 = ""
      }
    },

    //关闭弹窗
    beforeClose(done) {
      this.changeGuaranteePrice()
      this.resetQuery()
      this.$emit('refresh', true)
      done()
    },
    /**打开弹窗 */
    open(row) {
      this.getRemindRate(row)
      this.changeGuaranteePrice()
      this.rowData = row
      this.queryParams.khmcid = row.id
      this.$set(this.queryParams,'bxbl', row.bxbl)
      this.$set(this.queryParams,'jine2', delcommafy(row.jine2))
      //this.lockPrice = parseFloat(this.delcommafy(row.jine) / (row.bxbl * 0.01)).toFixed(AMOUNT_DIGIT)
      this.lockPrice = row.scx
      this.queryParams.allName = row.allName
      this.getList()
    },

    /**改变窗口状态 */
    changeGuaranteePrice() {
      this.openGuaranteePrice = !this.openGuaranteePrice;
    },
  
    /** 重置按钮操作 */
    resetQuery() {
      Object.assign(this.$data, this.$options.data())
    },

    updateForm() {
      if(this.queryParams.jine == '' || this.queryParams.jine == null) return  this.$modal.msgError("请输入正确的担保金额！") 
      if(parseFloat(this.queryParams.jine) - parseFloat(this.queryParams.jine2) >= 0) return this.$modal.msgError("担保金额大于提示金额！")
      this.$modal.confirm('是否修改？').then(() => {
        this.updateGuaranteedAmount();
      }).catch(() => {});
    }
  }
};
</script>
